package ruanjian.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;
import ruanjian.constant.RedisConstant;
import ruanjian.dao.SetmealDao;
import ruanjian.entity.PageResult;
import ruanjian.entity.QueryPageBean;
import ruanjian.pojo.Setmeal;
import ruanjian.service.SetmealService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealDao setmealDao;
    @Autowired
    private JedisPool jedisPool;

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        Page<Setmeal> page = setmealDao.selectByCondition(queryString);
        long total=page.getTotal();
        List<Setmeal> rows = page.getResult();
        return new PageResult(total,rows);
    }

    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        /*新增套餐信息，并且关联检查组信息*/
        setmealDao.add(setmeal);
        /*关联检查组和套餐id在t_setmeal_checkgroup表关联*/
        this.setSetmealAndCheckGroup(setmeal.getId(),checkgroupIds);
        //将图片名称保存到Redis集合中
        String filName=setmeal.getImg();
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,filName);
    }

    @Override
    public Setmeal findById(Integer id) {
        return setmealDao.findSetmealById(id);
    }

    @Override
    public List<Integer> findCheckGroupIdsBySetmealId(Integer id) {
        return setmealDao.findCheckGroupIdsBySetmealId(id);
    }

    @Override
    public void edit(Setmeal setmeal, Integer[] checkgroupIds) {
        setmealDao.edit(setmeal);
        Integer setmealId = setmeal.getId();
        setmealDao.deleteAssociation(setmealId);
        //重新建立当前检查组和检查项的关联关系
        this.setSetmealAndCheckGroup(setmealId,checkgroupIds);
    }

    @Override
    public void deleteById(Integer id) {
        //删除t_setmeal_checkgroup关联关系
        setmealDao.deleteAssociation(id);
        //根据ID删除t_setmeal中套餐
        setmealDao.deleteById(id);
    }

    @Override
    public List<Setmeal> findAll() {
        return setmealDao.findAll();
    }

    private void setSetmealAndCheckGroup(Integer setmealId, Integer[] checkgroupIds) {
        if(checkgroupIds!=null && checkgroupIds.length>0){
            Map<String,Integer> map=null;
            for (Integer checkgroupId : checkgroupIds) {
                map=new HashMap<>();
                map.put("setmealId",setmealId);
                map.put("checkgroupId",checkgroupId);
                setmealDao.setSetmealAndCheckGroup(map);
            }
        }
    }
}
